﻿package
{
	import fl.controls.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	public class flash extends Sprite
	{
		public static const URL:String = "http://htx.uit.edu.vn/online/log";
		
		private var _loader:URLLoader;
		private var _request:URLRequest;
		
		
		//PUBLIC HERE PLEASE
		public var ta_result:TextArea;
		public var btn_submit:Button;
		public var cmb_mamh:ComboBox;
		public var cmb_malop:ComboBox;
		public var cmb_magv:ComboBox;
		
		public function flash()
		{
			
			cmb_mamh=ComboBox(this.getChildByName("cmb_mamh"));
			cmb_mamh.tabIndex=1;
			
			cmb_malop=ComboBox(this.getChildByName("cmb_malop"));
			cmb_malop.tabIndex=2;
			
			cmb_magv=ComboBox(this.getChildByName("cmb_magv"));
			cmb_magv.tabIndex=3;
			
			btn_submit=Button(this.getChildByName("btn_submit"));
			btn_submit.tabIndex=4;
			btn_submit.addEventListener(MouseEvent.CLICK,btn_click);
			
			ta_result=TextArea(this.getChildByName("ta_result"));			
			
			loadData();
		}
		private function btn_click(e:MouseEvent)
		{			
			var mamh:String=this.cmb_mamh.value;
			var malop:String=this.cmb_malop.value;			
			var magv:String=this.cmb_magv.value;
			
				var myData:URLVariables = new URLVariables();
				myData.action="add";
				myData.mamh = mamh;
				myData.malop =malop;
				myData.magv=magv;
				
				
				_request = new URLRequest(URL);
				_request.data=myData;
				_request.method = URLRequestMethod.POST;
				
				
				_loader = new URLLoader();
				_loader.addEventListener(Event.COMPLETE, onLoadData);
				_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFiledToLoad);
				_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFiledToLoad);
				_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFiledToLoad);
				_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFiledToLoad);
				_loader.load(_request);
			
			
		}
		
		
			
		private function loadData():void {
			
			
			this.ta_result.text="";
			
			var myData:URLVariables = new URLVariables();
			myData.action="getlist";
			
			
			
			
			_request = new URLRequest(URL);
			_request.data=myData;
			_request.method = URLRequestMethod.POST;
			
			
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, onLoadData);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFiledToLoad);
			_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFiledToLoad);
			_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFiledToLoad);
			_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFiledToLoad);
			_loader.load(_request);
		}
		public function onLoadData(e:Event):void {
			var loader:URLLoader=URLLoader(e.target);
			var data:String=loader.data as String;
			
			
			var arr:Array=data.split("|");
			//trace(arr);
			var action:String=null;
			for(var i in arr)
			{
				if(i==0)
				{
					action=arr[i];
				}
				else
				{
					output(arr[i]);
				}
				
			}
			if(action!=null) trace(action+" me");
			
			
		}
		private function onDataFiledToLoad(e:IOErrorEvent):void {
			output("onDataFiledToLoad error=" + e.text);
		}
		public function error(msg:String):void
		{
			ExternalInterface.call("alert", msg);
			
		}
		public function output(obj:Object):void {
			
			ta_result.appendText(obj.toString());
			ta_result.appendText("\n");
		}
		
	}//class
}//end package